Skip to content

fix(babel): stop crashing on namespace and default imports - #5266

Open
xdimota7x wants to merge 1 commit into
mainfrom
fix/babel-namespace-default-imports
Open

xdimota7x wants to merge 1 commit into
mainfrom
fix/babel-namespace-default-imports

Conversation

@xdimota7x

Copy link
Copy Markdown
Collaborator

The plugin read specifier.imported.name for every specifier, but only named import specifiers have imported. A namespace or default import of react-native-paper failed the build with "Cannot read properties of undefined (reading 'name')".

Namespace and default imports now stay on the full index, while named imports in the same statement are still rewritten.

Fixes #5227

Motivation

With the optional Babel plugin (react-native-paper/babel) enabled, a file that imports Paper as a namespace or with a default import fails to compile:

Cannot read properties of undefined (reading 'name')

Apps usually enable the plugin only in the production Babel env, so the app works in development and the error first shows up in a release build. Named imports aren't affected.

After this change, namespace and default imports compile and load the full library. Named imports in the same statement are still rewritten. The Getting Started guide now says that a namespace import doesn't reduce the bundle size.

Related issue

Fixes #5227. This is the same crash as #3817, which was closed in 2024 without a fix.

Screenshots / Videos

N/A

Test plan

  1. Build the plugin with yarn prepack.

  2. From the repository root, run:

    node -e "
    const { transformSync } = require('@babel/core');
    for (const code of [
      \"import * as Paper from 'react-native-paper';\",
      \"import Paper, { Button } from 'react-native-paper';\",
    ]) {
      console.log(transformSync(code, { configFile: false, babelrc: false, plugins: ['./babel.js'] }).code);
    }
    "

    On main this throws Cannot read properties of undefined (reading 'name'). On this branch it prints:

    import * as Paper from "react-native-paper/lib/module/index.js";
    import Paper from "react-native-paper/lib/module/index.js";
    import Button from "react-native-paper/lib/module/components/Button/Button";

The plugin read `specifier.imported.name` for every specifier, but only
named import specifiers have `imported`. A namespace or default import of
react-native-paper failed the build with "Cannot read properties of
undefined (reading 'name')".

Namespace and default imports now stay on the full index, while named
imports in the same statement are still rewritten.

Fixes #5227
@github-actions

Copy link
Copy Markdown

Found potential problems with the pull request:

  • The description is too long. Please keep it under 1000 characters.
  • Screenshot or video evidence is missing. Make sure to include one if it affects the UI.

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 22a1f09

1 finding(s): 1 minor.

🟡 Minor (1)

Where Finding
src/babel/index.js:20 Default import of react-native-paper now compiles to an always-undefined binding

2 findings below minor hidden (user_settings.min_severity).

Reviewed with this repository's own Sentinel configuration (295f36b5e27b).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 1 new finding on 22a1f09. The verdict and the full list are in the summary comment.

Comment thread src/babel/index.js
Comment on lines +20 to +25
// Only a named import maps to a module. A namespace or default
// import (`import * as Paper`, `import Paper`) has no `imported`
// name, so it stays on the index.
const mapping = t.isImportSpecifier(specifier)
? mappings[specifier.imported.name]
: undefined;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Minor · Default import of react-native-paper now compiles to an always-undefined binding

import Paper from 'react-native-paper' compiles and rewrites to import Paper from "react-native-paper/lib/module/index.js", but neither src/index.tsx nor lib/module/index.js exports default, so Paper is undefined at runtime — the Paper.Button is not a component render error replaces the previous build-time failure. The new guard treats ImportDefaultSpecifier the same as a namespace import and keeps it on the index. The default-import and default-and-namespace-import fixtures lock this output in as expected behavior.

Suggested change

Either emit a plugin warning when a default specifier for react-native-paper is seen (e.g. path.buildCodeFrameError), or state in the getting-started note that react-native-paper has no default export so import Paper from 'react-native-paper' yields undefined.

Details

persona: javascript, senior-developer

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Babel plugin: a namespace or default import of react-native-paper fails the build

1 participant